package com.yy.personalcv.controller;

import com.yy.personalcv.pojo.UserInformation;
import com.yy.personalcv.service.UserInformationService;
import com.yy.personalcv.service.UserPasswordService;
import com.yy.personalcv.tool.StringUtils;
import com.yy.personalcv.tool.TokenProcessor;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;

@Controller
@Slf4j
public class UserController {

    @Resource
    private UserPasswordService userPasswordService;

    @Resource
    private UserInformationService userInformationService;


    @RequestMapping(value = "/login.do", method = RequestMethod.GET)
    public String login(HttpServletRequest request, Model model) {
        String token = TokenProcessor.getInstance().makeToken();
        log.info("用户登录token: " + token);
        request.getSession().setAttribute("token", token);
        model.addAttribute("token", token);
        return "login_page";
    }

    @RequestMapping(value = "/login.do", method = RequestMethod.POST)
    public String login(HttpServletRequest request,
                              @RequestParam String token, @RequestParam String username, @RequestParam String password) {

        String sessionToken = (String) request.getSession().getAttribute("token");
        if(StringUtils.getInstance().isNullOrEmpty(token)||!token.equals(sessionToken)){
            log.error("用户登录失败，token错误");
            return "redirect:/login.do";
        }
        boolean b= getUser(username, password, request);
        if(!b){
            log.error("用户登录失败，用户名或密码错误");
            return "redirect:/login.do";
        }
        return "redirect:/";
    }

    @RequestMapping(value = "/logout.do")
    public String logout(HttpServletRequest request){
        try{
            request.getSession().removeAttribute("userInformation");
            request.getSession().removeAttribute("uid");
            log.info("用户退出登录");
        }catch (Exception e){
            e.printStackTrace();
            return "redirect:/home.do";
        }
        return "redirect:/";
    }

    @RequestMapping(value = "getProfile.do")
    public String getProfile(HttpServletRequest request, Model model){
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        model.addAttribute("userInformation", userInformation);
        return "profile_page";
    }

    @RequestMapping(value = "/updateProfile.do", method = RequestMethod.GET)
    public String updateProfile(HttpServletRequest request, Model model){
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        model.addAttribute("userInformation", userInformation);
        return "update_profile_page";
    }

    @RequestMapping(value = "/updateProfile.do", method = RequestMethod.POST)
    public String updateProfile(HttpServletRequest request, Model model,
                                @RequestParam String username, @RequestParam String email, @RequestParam String phone, @RequestParam String avatar){
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        if(StringUtils.getInstance().isNullOrEmpty(username)||StringUtils.getInstance().isNullOrEmpty(email)||StringUtils.getInstance().isNullOrEmpty(phone)) {
            return "redirect:/updateProfile.do";
        }
        userInformation.setName(username);
        userInformation.setEmail(email);
        userInformation.setPhone(phone);
        userInformation.setAvatar(avatar);
        userInformationService.updateUserInformation(userInformation);
        request.getSession().setAttribute("userInformation", userInformation);
        return "redirect:/getProfile.do";
    }

    boolean getUser(String username, String password, HttpServletRequest request){
        //先判断用户是否存在，获得用户信息，存入session
        int uid = userInformationService.selectIdByName(username);
        if(uid == 0||StringUtils.getInstance().isNullOrEmpty(uid)){
            return false;
        }
        //判断密码是否正确
        UserInformation userInformation = userInformationService.selectByPrimaryKey(uid);
        if(userInformation == null){
            return false;
        }
        String userPassword = userPasswordService.selectByUid(userInformation.getId()).getPassword();
        password = StringUtils.getInstance().getMD5(password);
        if(!userPassword.equals(password)){
            return false;
        }
        request.getSession().setAttribute("userInformation", userInformation);
        request.getSession().setAttribute("uid", userInformation.getId());
        return true;
    }
}
